<?php
/**
 * ****************************************************************
 * 联麓短信API
 * 
 * 短信接口文档：https://www.shlianlu.com/console/document/api_4_2
 * DEMO下载：https://www.shlianlu.com/console/document/demo
 * 
 * @author Mr.RJH
 * @date 2025-09-18
 * @email<15213026209@163.com>
 * ****************************************************************
 */
namespace lianlu;

use app\model\sms\TextMessageContentTemplateModel;
use app\model\sms\TextMessageSignTemplateModel;
use think\facade\Log;

class TextMessageApi
{
    private $mchId = '1034528'; // 企业ID
    private $key = '108563bd106846b4a816942390a5709e'; // 通知短信key
    private $appId = '10011695091920663'; // 通知短信appid

    private $marketingKey = '3b09b7a615d9427cb9f8be18f04c1902'; // 营销短信key
    private $marketingAppId = '10021695091922876'; // 营销短信appid

    private $shortUrlAppId = '10081695091924614'; // 短链接APPid
    private $shortUrlKey = '16ebf8a7fd7443468b8e8a362fed6fbe'; // 短链接APPid
    private $host = 'https://apis.shlianlu.com';

    /**
     * 公有函数,设置请求头
     *
     * @return array
     */
    private function getHeader()
    {
        $arrCurlHeader = array(
            "Accept: application/json",
            "Content-Type: application/json;charset=UTF-8",
        );
        return $arrCurlHeader;
    }

    /**
     * 营销短信签名
     * 
     * @param array $params
     * @param int $type 1通知短信 2营销短信
     * @return string
     */
    private function getSign($params,$type)
    {
        // 计算数字签名
        if ($type == 1) {
            $key = $this->key; // 这里替换为实际的 appkey
        } else if ($type == 2) {
            $key = $this->marketingKey; // 这里替换为实际的 appkey
        } else if ($type == 3) {
            $key = $this->shortUrlKey;
        }
        
        // 按照请求中的参数名排序
        ksort($params);
        //去除相关参数
        $noSignKey = array("Signature", "ContextParamSet", "TemplateParamSet", "SessionContextSet", "PhoneNumberSet", "SessionContext", "PhoneList", "phoneSet");
        $signStr = '';
        //按顺序拼接
        foreach ($params as $k => $value) {
            if(!in_array($k, $noSignKey)) {
                $signStr .= "{$k}={$value}&";
            }
        }
        //最后拼接上appkey
        $signStr .= "key={$key}";
        
        //MD5加密后大写
        $signature = strtoupper(md5($signStr));
        return $signature;
    }

    /**
     * 短信调用Open Api，取回数据
     * 
     * @param int $type 1通知短信 2营销短信
     * @return json
     */
    private function getFetch($type)
    {

        $param['MchId'] = $this->mchId;
        if ($type == 1) {
            $param['AppId'] = $this->appId;
        } else if ($type == 2) {
            $param['AppId'] = $this->marketingAppId;
        } else if ($type == 3) {
            $param['AppId'] = $this->shortUrlAppId;
        }
        
        $param['Version'] = '1.1.0';
        $param['SignType'] = 'MD5';
        $param['TimeStamp'] = time();

        return $param;
    }

    /**
     * 基于联麓短信API 发送模板短信
     * 
     * @param array $PhoneNumberSet 发送手机号
     * @param string $TemplateId 模板ID
     * @param array $TemplateParamSet 模板变量内容["变量内容1","变量内容2","变量内容3"]，不带变量时使用[]
     * @param string $TaskTime 定时发送时间
     * @param string $tag 自定义标签 
     * @param int $type 
     * @return 
     */
    public function message_send(array $PhoneNumberSet, string $TemplateId, array $TemplateParamSet, int $type, string $TaskTime = '', string $tag = '')
    {
        $arrCurlData = $this->getFetch($type);
        $arrCurlData['Type'] = '3'; //短信类型，模板短信发送 固定值3
        $arrCurlData['PhoneNumberSet'] = $PhoneNumberSet;
        $arrCurlData['TemplateId'] = $TemplateId;
        $arrCurlData['TemplateParamSet'] = $TemplateParamSet;
        if ($TaskTime) {
            $arrCurlData['TaskTime'] = $TaskTime * 1000;
        }
        if ($tag) {
            $arrCurlData['Tag'] = $tag;
        }
        
        $arrCurlData['Signature'] = $this->getSign($arrCurlData, $type);
        $arrCurlData = json_encode($arrCurlData, JSON_UNESCAPED_UNICODE);
        
        $url = $this->host.'/sms/trade/template/send';

        $httpJson = http_post($url, $arrCurlData, $this->getHeader());
        $result = json_decode($httpJson, true);
        Log::write($result,'短信发送结果：');
        return $result;
    }

}